﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Payroll.Common.Utils;
using Payroll.BusinessLogic.Caching;
using Payroll.BusinessLogic.Data;
using Payroll.Common;

namespace Payroll.BusinessLogic.CustomerManager
{
    public partial class ResultService : IResultService
    {
        #region Fields

        /// <summary>
        /// Object context
        /// </summary>
        private readonly PayrollHRDataContext m_context;

        /// <summary>
        /// Cache manager
        /// </summary>
        private readonly ICacheManager _cacheManager;
       
        #endregion
        #region Ctor

        /// <summary>
        /// Ctor
        /// </summary>
        /// <param name="context">Object context</param>
        public ResultService(PayrollHRDataContext context)
        {
            this.m_context = context;
            this._cacheManager = new PayrollRequestCache();
        }

        #endregion
        #region Methods
        public List<Results> GetAllResult(string packageCode = null, string fromdate = null, string todate = null, string Distributor = null, string customerName = null, string taxcode = null, string mobile = null, string email = null, int? confrim = 3, int pageSize = int.MaxValue, int pageIndex = 0)
        {
            var query = from r in m_context.Results_GetDynamicContents(packageCode: packageCode, fromdate: fromdate, todate: todate, distributor : Distributor, custName: customerName, taxcode: taxcode, mobile: mobile, email: email, confirm: confrim.Value)
                        select new Results
                        {
                            ResultsId = r.ResultsId,
                            PackageCode = r.PackageCode ?? "N/A",
                            DatePurchase = r.DatePurchase,
                            CustomerName = r.CustomerName ?? "N/A",
                            TaxCode = r.TaxCode ?? r.TaxCode,
                            Address = r.Address ?? "N/A",
                            Mobile = r.Mobile ?? "N/A",
                            Email = r.Email ?? "N/A",
                            ServiceFree = r.ServiceFree ?? "N/A",
                            CustomerProfile = r.CustomerProfile ?? "N/A",
                            CreateDate = r.CreateDate,
                            Name = r.Name ?? "N/A"
                        };

            return query.ToList();
        }
        #endregion
        /// <summary>
        /// Deletes the specified result id.
        /// </summary>
        /// <param name="resultId">The result id.</param>
        /// <param name="packageCode">The package code.</param>
        /// <param name="fromdate">The fromdate.</param>
        /// <param name="todate">The todate.</param>
        /// <param name="customerName">Name of the customer.</param>
        /// <param name="taxcode">The taxcode.</param>
        /// <param name="mobile">The mobile.</param>
        /// <param name="email">The email.</param>
        /// <param name="pageSize">Size of the page.</param>
        /// <param name="pageIndex">Index of the page.</param>
        /// <returns></returns>
        public List<Results> Delete(string resultId, string packageCode = null, string fromdate = null, string todate = null, string Distributor = null, string customerName = null, string taxcode = null, string mobile = null, string email = null, int pageSize = int.MaxValue, int pageIndex = 0)
        {
            m_context.Result_Delete(resultId);
            return GetAllResult(packageCode: packageCode, fromdate: fromdate, todate: todate, Distributor: Distributor,
                customerName: customerName, taxcode: taxcode, mobile: mobile, email: email);
        }
        
        public void Save(Results result)
        {
            var item = (from r in m_context.Results
                       where r.ResultsId == result.ResultsId
                       select r).FirstOrDefault();
            if (!CommonHelper.IsNull(item))
            {
                item.PackageCode = result.PackageCode;
                item.DatePurchase = result.DatePurchase;
                item.CustomerName = result.CustomerName;
                item.TaxCode = result.TaxCode;
                //item.Address = result.Address;
                item.Mobile = result.Mobile;
                //item.Email = result.Email;
                //item.ServiceFree = result.ServiceFree;
                //item.CustomerProfile = result.CustomerProfile;
                item.AlterCustomerProfile = result.CustomerProfile;                
            }
            else {
                item = new Data.Result
                {
                    PackageCode = result.PackageCode,
                    DatePurchase = result.DatePurchase,
                    CustomerName = result.CustomerName,
                    TaxCode = result.TaxCode,
                    Address = result.Address,
                    Mobile = result.Mobile,
                    Email = result.Email,
                    ServiceFree = result.ServiceFree,
                    AlterCustomerProfile = result.CustomerProfile,
                    MessageID = result.MessageID
                };
                m_context.Results.InsertOnSubmit(item);
            }
            m_context.SubmitChanges();
        }

        public List<Results> GetbyMessageID(int MessageID, int Confirm)
        {
            var query = from r in m_context.Results_GetMessageID(messageID: MessageID, confirm: Confirm)
                        select new Results
                        {
                            ResultsId = r.ResultsId,
                            PackageCode = r.PackageCode ?? "N/A",
                            DatePurchase = r.DatePurchase,
                            CustomerName = r.CustomerName ?? "N/A",
                            TaxCode = r.TaxCode ?? r.TaxCode,
                            Address = r.Address ?? "N/A",
                            Mobile = r.Mobile ?? "N/A",
                            Email = r.Email ?? "N/A",
                            ServiceFree = r.ServiceFree ?? "N/A",
                            CustomerProfile = r.CustomerProfile ?? "N/A",
                            Name = r.Name ?? "N/A",
                            MessageID = r.MessageID
                        };
            return query.ToList();
        }

        public void Delete(int resultId)
        {
            m_context.Result_DeleteById(resultId: resultId);
        }

        public PagedList<CustomerAttribute> GetCustomerDevices(string Distributor = null, string customerName = null, string taxcode = null, string mobile = null
            , string email = null, int? Alert = null, int? ResStatus = 0, string Fromdate = null, string Todate = null, int pageSize = int.MaxValue, int pageIndex = 0)
        {
            var query = from c in m_context.Customer_GetDynamicContents(distributor: Distributor, customerName: customerName, taxcode: taxcode, mobile: mobile, email: email, alert: Alert, resStatus: ResStatus, startTime : Fromdate, endTime: Todate, pageSize: pageSize)
                        select new CustomerAttribute
                        {
                            ResultsId = c.ResultsId,
                            Distributor = c.Distributor,
                            CustomerName = c.CustomerName,
                            Address = c.Address,
                            Mobile = c.Mobile,
                            Email = c.Email,
                            DatePurchase = c.DatePurchase,
                            EndDate = c.EndDate,
                            Alert = c.Alert.Value,
                            PackageCode = c.PackageCode,
                            PackageTimeName = c.PackageTimeName,
                            PackageTypeName = c.PackageTypeName,
                            TaxCode = c.TaxCode
                        };
                return new PagedList<CustomerAttribute>(query.ToList(), pageIndex, pageSize);            
        }

        public List<CustomerAttribute> Delete(string ResultsId, string Distributor = null, string customerName = null, string taxcode = null, string mobile = null, string email = null, int? Alert = null, int? ResStatus = 0, string Fromdate = null, string Todate = null, int pageSize = int.MaxValue, int pageIndex = 0)
        {
            m_context.Customer_Delete(resultsId: ResultsId);
            return this.GetCustomerDevices(Distributor, customerName, taxcode, mobile, email, Alert, ResStatus, Fromdate, Todate, pageSize, pageIndex);
        }


        public List<CustomerAttribute> Confirm(string ResultsId, string Distributor = null, string customerName = null, string taxcode = null, string mobile = null, string email = null, int? Alert = null, int? ResStatus = 0, string Fromdate = null, string Todate = null, int pageSize = int.MaxValue, int pageIndex = 0)
        {
            m_context.Customer_Confirm(resultsId: ResultsId);
            return this.GetCustomerDevices(Distributor, customerName, taxcode, mobile, email, Alert, ResStatus, Fromdate, Todate, pageSize, pageIndex);
        }


        public void SaveCustomer(Results result)
        {
            var item = m_context.Results.Where(r => r.ResultsId == result.ResultsId).FirstOrDefault();
            if (!CommonHelper.IsNull(item))
            {
                item.CustomerName = result.CustomerName;
                item.DatePurchase = result.DatePurchase;
                item.TaxCode = result.TaxCode;
                item.Address = result.Address;
                item.PackageCode = result.PackageCode;
                item.Mobile = result.Mobile;
                m_context.SubmitChanges();
            }
        }
    }
}
